package com.youhome.service.service.impl;


import com.youhome.service.service.ChartService;
import com.youhome.service.utils.ChartUtil;
import com.youhome.service.utils.CustomRender;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.data.category.DefaultCategoryDataset;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.util.UUID;

/**
 * Created by chen365 on 2018/08/29.
 */
@Service
public class ChartServiceImpl implements ChartService {
    @Override
    public String test() {
        StandardChartTheme standardChartTheme = new StandardChartTheme("CN"); //创建主题样式
        standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20)); //设置标题字体
        standardChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15)); //设置图例的字体
        standardChartTheme.setLargeFont(new Font("宋体", Font.PLAIN, 15)); //设置轴向的字体
        ChartFactory.setChartTheme(standardChartTheme);//设置主题样式

        DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
        dataSet.addValue(6000, "1月", "水果");
        dataSet.addValue(3000, "2月", "水果");
        dataSet.addValue(12000, "3月", "水果");
        dataSet.addValue(12000, "4月", "水果");
        dataSet.addValue(12000, "5月", "水果");
        dataSet.addValue(12000, "6月", "水果");


        // 通过ChartFactory创建JFreeChart

        JFreeChart chart = ChartFactory.createBarChart(
                "此房源价格与附近均价对比", //图表标题
                "", //横轴标题
                "",//纵轴标题
                dataSet,//数据集合
                PlotOrientation.VERTICAL, //图表方向
                false,//是否显示图例标识
                false,//是否显示tooltips
                false);//是否支持超链接

        chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        chart.setTextAntiAlias(false);

        chart.getTitle().setFont(new Font("微软雅黑", Font.PLAIN, 20)); // 设置标题字体
        chart.setBorderVisible(true); // 设置显示边框
        CategoryPlot plot = chart.getCategoryPlot(); // 获取绘图区对象
        plot.setForegroundAlpha(0.8F);//设置绘图区前景色透明度
        plot.setBackgroundAlpha(0.5F);//设置绘图区背景色透明度

        CategoryAxis categoryAxis = plot.getDomainAxis();//获取坐标轴对象
        categoryAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));//设置坐标轴标题字体
        categoryAxis.setTickLabelFont(new Font("微软雅黑", Font.PLAIN, 12));//设置坐标轴尺值字体
        categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);// 设置坐标轴标题旋转角度

        ValueAxis valueAxis = plot.getRangeAxis();// 设置数据轴对象
        valueAxis.setLabelFont(new Font("微软雅黑", Font.PLAIN, 12));



        BarRenderer renderer = new BarRenderer();
        renderer.setSeriesPaint(0, Color.decode("#E46C0A")); // 给series2 Bar
        renderer.setSeriesPaint(1, Color.decode("#C0504D")); // 给series1 Bar
        renderer.setSeriesPaint(2, Color.decode("#4F81BD")); // 给series3 Bar
//        renderer.setSeriesPaint(3, Color.decode("#00B050")); // 给series4 Bar
//        renderer.setSeriesPaint(4, Color.decode("#7030A0")); // 给series5 Bar
//        renderer.setSeriesPaint(5, Color.decode("#00BF00")); // 给series6 Bar

        // 设置柱状图的顶端显示数字
        renderer.setIncludeBaseInRange(true);
        //renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setDefaultItemLabelsVisible(true);
        // 设置柱子为平面图不是立体的
        renderer.setBarPainter(new StandardBarPainter());

        // 设置柱状图之间的距离0.1代表10%；
        //renderer.setItemMargin(0.01);
        // 设置柱子的阴影，false代表没有阴影
        renderer.setShadowVisible(false);

        //renderer.setMaximumBarWidth(0.08); // 宽度
        //renderer.setMinimumBarLength(0.5); //长度

        plot.setRenderer(renderer);

        String filename = UUID.randomUUID().toString() + ".png";

        ChartUtil.saveAsFile(chart, "jpg","/alldata/chart/" + filename, 800, 500);


        return "/chart/" + filename;
    }

    @Override
    public String createBarChart(String title, DefaultCategoryDataset dataSet) {
        StandardChartTheme standardChartTheme = new StandardChartTheme("unicode"); //创建主题样式
        standardChartTheme.setExtraLargeFont(new Font("宋体", Font.BOLD, 20)); //设置标题字体
        standardChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15)); //设置图例的字体
        standardChartTheme.setLargeFont(new Font("宋体", Font.PLAIN, 15)); //设置轴向的字体
        ChartFactory.setChartTheme(standardChartTheme);//设置主题样式

        // 通过ChartFactory创建JFreeChart
        JFreeChart chart = ChartFactory.createBarChart(
                title, //图表标题
                "", //横轴标题
                "",//纵轴标题
                dataSet,//数据集合
                PlotOrientation.VERTICAL, //图表方向
                false,//是否显示图例标识
                false,//是否显示tooltips
                false);//是否支持超链接


        chart.setTextAntiAlias(false);
        chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
        chart.getTitle().setFont(new Font("宋体", Font.PLAIN, 20)); // 设置标题字体
        chart.setBorderVisible(true); // 设置显示边框
        CategoryPlot plot = chart.getCategoryPlot(); // 获取绘图区对象
        plot.setForegroundAlpha(0.8F);//设置绘图区前景色透明度
        plot.setBackgroundAlpha(0.9F);//设置绘图区背景色透明度

        CategoryAxis categoryAxis = plot.getDomainAxis();//获取坐标轴对象
        categoryAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));//设置坐标轴标题字体
        categoryAxis.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));//设置坐标轴尺值字体
        //categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);// 设置坐标轴标题旋转角度

        ValueAxis valueAxis = plot.getRangeAxis();// 设置数据轴对象
        valueAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));

        BarRenderer renderer = new CustomRender();

        // 设置柱状图的顶端显示数字
        renderer.setIncludeBaseInRange(true);
        renderer.setDefaultItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setDefaultItemLabelsVisible(true);
        // 设置柱子为平面图不是立体的
        renderer.setBarPainter(new StandardBarPainter());

        // 设置柱状图之间的距离0.1代表10%；
        renderer.setItemMargin(0.01);
        // 设置柱子的阴影，false代表没有阴影
        renderer.setShadowVisible(false);

        renderer.setMaximumBarWidth(0.1); // 宽度
        renderer.setMinimumBarLength(0.0); //长度

        plot.setRenderer(renderer);

        String filename = UUID.randomUUID().toString() + ".png";
        ChartUtil.saveAsFile(chart, "jpg","/alldata/chart/" + filename, 400, 250);

        return "/chart/" + filename;
    }

}
